package com.bs.jianli.util;

import com.bs.jianli.util.interceptor.MyInterceptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.util.List;

/**
 * @author 小姚
 * @pakeageName:com.bs.shopping.utils
 * @ClassName:JsonConfig
 * @Descirption:
 * @data 2022/11/18 22:52
 */
@Configuration
@EnableWebMvc
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
public class JsonConfig implements WebMvcConfigurer {
    @Value("${upload.path}")
    private String pathName;
    @Value("${upload.parttern}")
    private String pathNames;


    //解决前后端跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  // 拦截所有的请求
                .allowedOrigins("http://localhost:3001/")  // 可跨域的域名，可以为 *
                .allowCredentials(true)
                .allowedMethods("*")   // 允许跨域的方法，可以单独配置
                .allowedHeaders("*");  // 允许跨域的请求头，可以单独配置
    }

    //解决前后修改时，获取的id精度丢失问题
    @Override
    public void
    configureMessageConverters(List<HttpMessageConverter
                <?>> converters) {
        MappingJackson2HttpMessageConverter
                jackson2HttpMessageConverter = new
                MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new
                ObjectMapper();
        SimpleModule simpleModule = new
                SimpleModule();
        simpleModule.addSerializer(Long.class,
                ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE,
                ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(jackson2HttpMessageConverter);
    }

    //解决无法访问swagger页面
    @Override
    public void
    addResourceHandlers(ResourceHandlerRegistry
                                registry) {
// 解决静态资源无法访问
        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/",
                "classpath:/resources/", "classpath:/static/",
                "classpath:/public/");
// 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
// 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler(pathNames).addResourceLocations("file:"+pathName);
    }

   /* @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login","/user/add");
    }*/
}
